package cn.yuanqiao.manage.service.impl;

import cn.yuanqiao.manage.domain.YqdaApplyModel;
import cn.yuanqiao.manage.mapper.YqdaApplyModelMapper;
import cn.yuanqiao.manage.service.IYqdaApplyModelService;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * applymodelService业务层处理
 *
 * @author 赵常飞
 * @date 2023-06-16
 */
@Service
public class YqdaApplyModelServiceImpl implements IYqdaApplyModelService
{
    @Autowired
    private YqdaApplyModelMapper yqdaApplyModelMapper;

    /**
     * 查询applymodel
     *
     * @param menuId 菜单id
     * @param categoryId 门类id
     * @return applymodel
     */
    @Override
    public YqdaApplyModel selectYqdaApplyModelById(Long menuId, Long categoryId)
    {
        return yqdaApplyModelMapper.selectYqdaApplyModelById(menuId,categoryId);
    }

    /**
     * 查询applymodel列表
     *
     * @param yqdaApplyModel applymodel
     * @return applymodel
     */
    @Override
    public List<YqdaApplyModel> selectYqdaApplyModelList(YqdaApplyModel yqdaApplyModel)
    {
        return yqdaApplyModelMapper.selectYqdaApplyModelList(yqdaApplyModel);
    }

    /**
     * 新增applymodel
     *
     * @param yqdaApplyModel applymodel
     * @return 结果
     */
    @Override
    public int insertYqdaApplyModel(YqdaApplyModel yqdaApplyModel)
    {
        List<YqdaApplyModel> yqdaApplyModels = yqdaApplyModelMapper.selectYqdaApplyModelList(yqdaApplyModel);
        if(yqdaApplyModels.size()==0){//没有相关数据则增加
            return yqdaApplyModelMapper.insertYqdaApplyModel(yqdaApplyModel);
        }else{//存在则修改
            return yqdaApplyModelMapper.updateYqdaApplyModel(yqdaApplyModel);
        }

    }

    /**
     * 修改applymodel
     *
     * @param yqdaApplyModel applymodel
     * @return 结果
     */
    @Override
    public int updateYqdaApplyModel(YqdaApplyModel yqdaApplyModel)
    {
        return yqdaApplyModelMapper.updateYqdaApplyModel(yqdaApplyModel);
    }

    /**
     * 批量删除applymodel
     *
     * @param categoryId 门类id
     * @param menuId 菜单id
     * @return 结果
     */
    @Override
    public int deleteYqdaApplyModel(Long categoryId,Long menuId)
    {
        return yqdaApplyModelMapper.deleteYqdaApplyModel(categoryId,menuId);
    }

    /**
     * 删除applymodel信息
     *
     * @param Id applymodel主键
     * @return 结果
     */
    @Override
    public int deleteYqdaApplyModelById(Long Id)
    {
        return yqdaApplyModelMapper.deleteYqdaApplyModelById(Id);
    }

    @Override
    public int clonefield(JSONObject json) {
        Long categoryId=json.getLong("categoryId");
        Long clonedmenuId=json.getLong("clonedmenuId");
        JSONArray clonetomenuIds=json.getJSONArray("clonetomenuIds");
        return yqdaApplyModelMapper.clonefield(categoryId,clonedmenuId,clonetomenuIds);
    }

    @Override
    public List<Map<String, String>> selectYqdaApplyModelListmenu(String menuName,Long categoryId) {
        return yqdaApplyModelMapper.selectYqdaApplyModelListmenu(menuName,categoryId);
    }

    @Override
    public String getfieldlist(Long menuId,Long categoryId,String field) {
        return yqdaApplyModelMapper.getfieldlist(menuId,categoryId,field);
    }

    @Override
    public List<Map<String, String>> getmenulist(JSONObject json) {
        return yqdaApplyModelMapper.getmenulist(json.getLong("categoryId"));
    }

    @Override
    public int addmultiplemenu(JSONObject json) {
        JSONArray menuids = json.getJSONArray("menuids");
        Long categoryId=json.getLong("categoryId");
        for (int i = 0; i < menuids.size(); i++) {
            YqdaApplyModel yqdaApplyModel1 = yqdaApplyModelMapper.selectYqdaApplyModelById(Long.valueOf(menuids.get(i).toString()), categoryId);
            if(null!=yqdaApplyModel1){
                continue;
            }
            //此处不判断是否存在,需在查询时排除
            YqdaApplyModel yqdaApplyModel=new YqdaApplyModel();
            yqdaApplyModel.setCategoryId(categoryId);
            yqdaApplyModel.setMenuId(Long.valueOf(menuids.get(i).toString()));
            yqdaApplyModelMapper.insertYqdaApplyModel(yqdaApplyModel);
        }
        return 1;
    }

    @Override
    public List<Map<String, String>> getmenu(Long categoryId) {
        return yqdaApplyModelMapper.getmenu(categoryId);
    }

    @Override
    public List<Map<String, String>> getmdatalist(JSONObject json) {
        String field=json.getString("field");
        Long categoryId=json.getLong("categoryId");
        Integer type=json.getInteger("type");
        Long menuId = json.getLong("menuId");
        String sql="";
        if(field.toUpperCase().contains("LIST")){//展示字段
            sql=" and is_hidden=0 ";
        }else if(field.toUpperCase().contains("ADD")){//添加字段
            sql=" and is_input=1 ";
        }else if(field.toUpperCase().contains("UPDATE")){//修改字段
            sql=" and is_update=1 ";
        }else if(field.toUpperCase().contains("EXPORT")){//导出字段
            sql=" and IS_EXPORT=1 ";
        }else if(field.toUpperCase().contains("QUERY")) {//查询字段
            sql = " and IS_QUERY!=0 ";
        }else{
            sql = " and 1=1 ";
        }
        String sql2="select "+field+" from YQDA_APPLY_MODEL where menu_id="+menuId+" and category_id="+categoryId;
        /*String nr=yqdaApplyModelMapper.getSingle(sql2);
        if(null==nr||"".equals(nr)){

        }else{
            nr="and FIELD_NAME not in("+"'"+nr.replace(",","','")+"'"+")";
        }*/
        return yqdaApplyModelMapper.getmdatalist(field,categoryId,type,sql);
    }

    @Override
    public int deletebycatemenu(com.alibaba.fastjson.JSONObject json) {
        Long categoryId=json.getLong("categoryId");
        com.alibaba.fastjson.JSONArray menuIds=json.getJSONArray("menuIds");
        return yqdaApplyModelMapper.deletebycatemenu(categoryId,menuIds);
    }
}
